<template>
  <WsmTable
    ref="table"
    load-url="/dbms-server/base/dictionary/page"
    :change-load="true"
    v-model="queryFormType"
  >
    <template #title>{{ t('dbms.dicManager.title') }}</template>
    <template #button="{ row }">
      <el-button type="primary" v-hasPermi="'add'" @click="addForm.show()">{{
        t('common.add')
      }}</el-button>
      <template v-if="row">
        <el-button v-hasPermi="'update'" @click="update(row.code)" type="primary">{{
          t('common.update')
        }}</el-button>
        <el-button v-hasPermi="'delete'" @click="del(row.code)" type="danger">{{
          t('common.delete')
        }}</el-button>
        <el-button v-hasPermi="'config'" @click="config(row.code)" type="warning">{{
          t('dbms.dicManager.config')
        }}</el-button>
        <template v-if="row.activity">
          <el-button v-hasPermi="'check'" @click="check(row.code)" type="danger">{{
            t('dbms.dicManager.disable')
          }}</el-button>
        </template>
        <template v-else>
          <el-button v-hasPermi="'check'" @click="check(row.code)" type="success">{{
            t('dbms.dicManager.enable')
          }}</el-button>
        </template>
      </template>
    </template>
    <template #column>
      <el-table-column prop="activity" :label="t('dbms.dicManager.activity')" width="100">
        <template #default="scope">
          <template v-if="scope.row.activity">
            <el-tag effect="dark" type="success">{{ t('dbms.dicManager.enable') }}</el-tag>
          </template>
          <template v-else>
            <el-tag effect="dark" type="danger">{{ t('dbms.dicManager.disable') }}</el-tag>
          </template>
        </template>
      </el-table-column>
      <el-table-column prop="code" :label="t('dbms.dicManager.code')" width="180" />
      <el-table-column prop="name" :label="t('dbms.dicManager.name')" width="180" />
      <el-table-column prop="note" :label="t('dbms.dicManager.note')" />
    </template>
  </WsmTable>
  <WsmForm
    ref="addForm"
    v-model="addData"
    submit-url="/dbms-server/base/dictionary/add"
    label-width="100px"
    :title="t('dbms.dicManager.addTitle')"
    @success="flushed"
  >
    <template #default="{ form }">
      <el-row>
        <el-col :span="12">
          <el-form-item prop="code" :label="t('dbms.dicManager.code')">
            <el-input v-model="form.code" :placeholder="t('dbms.dicManager.code')" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item prop="name" :label="t('dbms.dicManager.name')">
            <el-input v-model="form.name" :placeholder="t('dbms.dicManager.name')" />
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="24">
          <el-form-item prop="note" :label="t('dbms.dicManager.note')">
            <el-input
              type="textarea"
              v-model="form.note"
              :placeholder="t('dbms.dicManager.note')"
            />
          </el-form-item>
        </el-col>
      </el-row>
    </template>
  </WsmForm>

  <WsmForm
    ref="updateForm"
    v-model="updateData"
    load-url="/dbms-server/base/dictionary/get"
    label-width="100px"
    submit-url="/dbms-server/base/dictionary/update"
    :title="t('dbms.dicManager.updateTitle')"
    @success="flushed"
  >
    <template #default="{ form }">
      <el-row>
        <el-col :span="24">
          <el-form-item prop="name" :label="t('dbms.dicManager.name')">
            <el-input v-model="form.name" :placeholder="t('dbms.dicManager.name')" />
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="24">
          <el-form-item prop="note" :label="t('dbms.dicManager.note')">
            <el-input
              type="textarea"
              v-model="form.note"
              :placeholder="t('dbms.dicManager.note')"
            />
          </el-form-item>
        </el-col>
      </el-row>
    </template>
  </WsmForm>
  <DicValue ref="dicConfig" />
</template>
<script setup lang="ts">
import { WsmTable } from '@/components/WsmTable'
import { WsmForm } from '@/components/WsmForm'
import { QueryFormType, QueryType } from '@/components/WsmTable/src/types'
import { checkstateApi, delApi } from '@/api/dbms/dicmanager'
import { ref } from 'vue'
import { ElMessageBox } from 'element-plus'
import { useI18n } from '@/hooks/web/useI18n'
import { FormDataType } from '@/components/WsmForm/src/types'

import DicValue from './components/DicValue.vue'

const { t } = useI18n()

//表格对象
const table = ref()

//新增表单对象
const addForm = ref()
const addData: FormDataType = {
  code: '',
  name: '',
  note: ''
}

//修改表单对象
const updateForm = ref()
const updateData: FormDataType = {
  code: '',
  name: '',
  note: ''
}

//查询定义
const queryFormType: QueryFormType = {
  activity: {
    value: '',
    queryType: QueryType.EQ
  }
}

const update = (code: string) => {
  updateForm.value.show({ code })
}
const del = async (code: string) => {
  ElMessageBox.confirm(t('common.delMessage'), t('common.delWarning'), {
    confirmButtonText: t('common.ok'),
    cancelButtonText: t('common.cancel'),
    type: 'warning'
  }).then(async () => {
    const rs = await delApi({ code })
    if (rs.success) {
      table.value.load(1)
    }
  })
}
const check = async (code: string) => {
  const rs = await checkstateApi(code)
  if (rs.success) {
    table.value.load()
  }
}

const dicConfig = ref()

const config = (code: string) => {
  dicConfig.value.show(code)
}

const flushed = () => {
  table.value.load(1)
}
</script>

<style scoped></style>
